package com.shinechain.controller;

import com.shinechain.botModel.ShineChainBot;
import com.shinechain.dao.*;
import com.shinechain.domain.OpenRedPaperReq;
import com.shinechain.domain.OpenRedPaperResp;
import com.shinechain.model.BotAd;
import com.shinechain.model.BotChat;
import com.shinechain.model.ResponseJson;
import com.shinechain.utils.PatternChat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


@RequestMapping(value = "api")
public class ProjectBotController implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    PatternChat patternChat;

    /**
     * 自动注入
     *
     */
    @Autowired
    BotAdMapper botAdMapper;
    /**
     * 自动注入
     *
     */
    @Autowired
    BotChatMapper botChatMapper;

    @Autowired
    BotKitManMapper botKitManMapper;

    @Autowired
    RewardMapper rewardMapper;

    @Autowired
    RewardUserMapper rewardUserMapper;




    @RequestMapping(value = "setAd")
    @ResponseBody
    public List<BotAd> setAd(@RequestBody BotAd botAd){

        if(botAd!=null) {

            botAdMapper.insert(botAd);

            patternChat.updateList();
        }
        return botAdMapper.findAll();
    }

    @RequestMapping(value = "deleteAd")
    @ResponseBody
    public List<BotAd> deleteAd(HttpServletRequest request){

        if(request!=null) {

            botAdMapper.deleteByPrimaryKey(Integer.parseInt(request.getParameter("id")));

            patternChat.updateList();
        }
        return botAdMapper.findAll();
    }

    @RequestMapping(value = "setChat")
    @ResponseBody
    public List<BotChat> setChat(@RequestBody BotChat botChat){
        botChat.setId(null);

        botChatMapper.insert(botChat);

        patternChat.updateMap();

        return botChatMapper.findAll();
    }

    @RequestMapping(value = "deleteChat")
    @ResponseBody
    public List<BotChat> deleteChat(HttpServletRequest request){

        botChatMapper.deleteByPrimaryKey(Integer.parseInt(request.getParameter("id")));

        patternChat.updateMap();

        return botChatMapper.findAll();
    }



    @RequestMapping(value = "getAll")
    @ResponseBody
    public ResponseJson getAll(){
        ResponseJson responseJson = new ResponseJson();
        responseJson.setBotAdList(botAdMapper.findAll());
        responseJson.setBotChatList(botChatMapper.findAll());
        responseJson.setBotKitManList(botKitManMapper.findAll());

        return responseJson;
    }

    @RequestMapping(value = "start")
    @ResponseBody
    public String start(){
        patternChat.updateList();
        patternChat.updateMap();
        return "ture";

    }

    @RequestMapping(value = "getBotChat")
    @ResponseBody
    public BotChat getBotChat(){
       BotChat botChat = new BotChat();
       botChat.setKeyword("你是谁");
       botChat.setResponse("我是bot");
        return botChat;

    }

    @RequestMapping(value = "getAdList")
    @ResponseBody
    public List<BotAd> getBotChatList(){

        BotAd botAd = new BotAd();
        BotAd botAd1 = new BotAd();

        botAd.setAd("linfinity");
        botAd1.setAd("广告");
        List<BotAd> list = new ArrayList<>();
        list.add(botAd);
        list.add(botAd1 );

        return list;

    }





    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        if(event.getApplicationContext().getParent() == null)//root application context 没有parent，他就是老大.
        {
            try{
                ShineChainBot.rewardMapper = this.rewardMapper;
                ShineChainBot.rewardUserMapper = this.rewardUserMapper;
                patternChat.updateList();
                patternChat.updateMap();

            }catch (Exception e){
                e.printStackTrace();

            }
        }
    }
}
